<?php

/**
 * Joomla! 1.5 component arts_curriculum
 * @package    arts_curriculum
 * @subpackage
 * @author     Chris Borkowski {@link http://www.guggenheim.org}
 * @author     Created on 09-December-2009
 */

// no direct access
defined( '_JEXEC' ) or die( 'Restricted access' );

jimport( 'joomla.application.component.controller' );

// we're gonna need some help for the drop downs
require_once( JPATH_COMPONENT.DS.'helpers'.DS.'helper.php' );

/**
 * Arts_curriculum Controller
 *
 * @package Arts_curriculum
 */
class Arts_curriculumControllerVocab extends JController
{
	
	function __construct()
	{
		parent::__construct();
		// Register Extra tasks
		$this->registerTask( 'add', 'edit' );
		$this->registerTask('apply', 'save');
	
	}
	
	function save()
	{
		JRequest::checkToken() or jexit( 'Invalid Token' );
		
		global $option;
		
		$row =& JTable::getInstance('vocab_item', 'Table');

		if (!$row->bind(JRequest::get('post'))) 
		{
			JError::raiseError(500, $row->getError() );
		}
		
		$row->main_body = JRequest::getVar( 'main_body', '', 'post', 'string', JREQUEST_ALLOWRAW );
		$row->additional_resources = JRequest::getVar( 'additional_resources', '', 'post', 'string', JREQUEST_ALLOWRAW );
		
		
		//let's handel the upload - ugly but's it 's my first uploader
		
		$file = JRequest::getVar( 'pdf', '', 'files', 'array' );

		if(isset($file['name']) && $file['name'] != '')
		{	
			jimport('joomla.filesystem.file');
			
			$safe = JFile::makesafe($file['name']);
			$bads = array(" ", "?", "*", "+", "@", "#", "%", "$", "!", "&");
			$cleaned = str_replace($bads, "_", $safe);
			$filename = JPATH_SITE . DS . 'images' . DS . 'content' . DS . 'arts_curriculum' . DS . 'assets'. DS. $cleaned;	
				
				if (!JFile::upload($file['tmp_name'], $filename)) {
				$this->msg = JText::_( 'Life is bad and the upload failed, check to make sure that /images/content/arts_curriculum/images/assets exists and is writable' );
				return;
			}
			
			// were' in the file system so upadte the DB		
			$db =& JFactory::getDBO();
			$query = "UPDATE #__arts_curriculum_vocb SET pdf = '$cleaned' WHERE id = $this->id ";
			$db->setQuery( $query );
			if (!$db->query()) {
				echo "<script> alert('".$db->getErrorMsg()."'); window. history.go(-1); </script>\n";
			}

			
		}
		
		if (!$row->store()) 
		{
			JError::raiseError(500, $row->getError() );
		}
		
	    if ($this->getTask() == 'apply') {
			$this->setRedirect('index.php?option=' . $option . '&task=edit&controller=vocab&cid[]=' . $row->id, 'Changes Applied');
		} else {
			$this->setRedirect('index.php?option=' . $option . '&controller=vocab', 'Vocabulary Item Saved');
		}
	
	
	}
	
	function remove()
	{
		JRequest::checkToken() or jexit( 'Invalid Token' );
		
		global $option;
		
		$cid = JRequest::getVar('cid', array(0));
		
		$row =& JTable::getInstance('vocab', 'Table');
		
		foreach ($cid as $id) {
			$id = (int) $id;
			
			if (!$row->delete($id)) {
				JError::raiseError(500, $row->getError() );
			}
		}
		
		$s = '';
		
		if (count($cid) > 1) {
			$s = 's';
		}
		
		$this->setRedirect('index.php?option=' . $option . '&controller=vocab', 'Vocabulary Item' . $s . ' deleted.');
	}
	
	function display()
	{
		$view = JRequest::getVar('view');
		
		if (!$view) {
			switch ($this->getTask()) {
				case 'edit':
					JRequest::setVar('view', 'vocab_item');
					break;
					
				case 'add':
					JRequest::setVar('view', 'vocab_item');
					break;
				
				default:
					JRequest::setVar('view', 'vocab');
					break;
			}
		}

		parent::display();
	}
}
?>
